﻿//TagulCloud
package tagul 
{
	import __AS3__.vec.Vector;
	
	import flash.display.GraphicsPath;
	import flash.display.GraphicsPathCommand;
	import flash.display.GraphicsSolidFill;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	
	default xml namespace = "http://www.w3.org/2000/svg";
	
    public class TagulCloud extends Sprite
    {
        public function TagulCloud()
        {
            super();
            this.stringHelper = new StringHelper();
            this.backgroundColor = 16777215;
            return;
        }

        private function getColor(arg1:String, arg2:Number=0):Number
        {
            return Number("0x" + this.stringHelper.trim(arg1).toLowerCase().substr(arg2));
        }

        public function parseANDdraw(svg:XML, popup:Boolean=false):void
        {
            var loc7:*=null;
            var loc8:*=null;
            var loc9:*=null;
            var loc10:*=null;
            var loc11:*=null;
            var loc12:*=null;
            var loc13:*=null;
            var loc14:*=null;
            var tag:Tag=null;
            var loc16:*=null;
            var xlink:Namespace=new Namespace("http://www.w3.org/1999/xlink");
            var t:Namespace=new Namespace("http://www.tagul.com/schema");
            var nsdef:Namespace=new Namespace("http://www.w3.org/2000/svg");
            var loc3:*=String(svg.@viewBox).split(" ");
            
            this.viewBox = new Rectangle(loc3[0], loc3[1], loc3[2], loc3[3]);
            var loc4:*=svg.t::styleOptions;
            var loc5:*={
            	"animationSpeed":String(loc4.t::animationSpeed) != "" ? String(loc4.t::animationSpeed) : 0.15, 
            	"rollOverTextColor":String(loc4.t::textColor) != "" ? new GraphicsSolidFill(this.getColor(loc4.t::textColor),String(loc4.t::textAlpha) != "" ? loc4.t::textAlpha : 1) : null, 
            	"rollOverBoxColor":String(loc4.t::boxColor) != "" ? new GraphicsSolidFill(this.getColor(loc4.t::boxColor), String(loc4.t::boxAlpha) != "" ? loc4.t::boxAlpha : 1) : new GraphicsSolidFill(0, 0), 
            	"rollOverBoxStroke":String(loc4.t::boxStrokeColor) != "" ? {"weight":String(loc4.t::boxStrokeWeight) != "" ? loc4.t::boxStrokeWeight : 0, "color":this.getColor(loc4.t::boxStrokeColor), "alpha":String(loc4.t::boxStrokeAlpha) != "" ? loc4.t::boxStrokeAlpha : 1} : null, 
            	"rollOverRotate":String(loc4.t::rotate) == "true" || String(loc4.t::rotate) == "1" ? true : false, 
            	"rollOverZoom":String(loc4.t::zoom) == "true" || String(loc4.t::zoom) == "1" ? true : false};
            this.backgroundColor = String(loc4.t::animationSpeed) != "" ? Number("0x" + String(loc4.t::backgroundColor)) : 16777215;
            var loc6:*=new Object();
            var loc17:*=0;
            var loc18:*=svg.nsdef::defs.*;
            for each (loc7 in loc18) 
            {
                loc9 = String(loc7.path.@d).split(" ");
                loc10 = new GraphicsPath(new Vector.<int>,new Vector.<Number>);
                while (loc9.length) 
                {
                    if ((loc11 = loc9.shift()) == "M") 
                    {
                        loc10.commands.push(GraphicsPathCommand.MOVE_TO);
                        loc10.data.push(parseFloat(loc9.shift()));
                        loc10.data.push(parseFloat(loc9.shift()));
                        continue;
                    }
                    if (loc11 == "Q") 
                    {
                        loc10.commands.push(GraphicsPathCommand.CURVE_TO);
                        loc10.data.push(parseFloat(loc9.shift()));
                        loc10.data.push(parseFloat(loc9.shift()));
                        loc10.data.push(parseFloat(loc9.shift()));
                        loc10.data.push(parseFloat(loc9.shift()));
                        continue;
                    }
                    if (loc11 == "L") 
                    {
                        loc10.commands.push(GraphicsPathCommand.LINE_TO);
                        loc10.data.push(parseFloat(loc9.shift()));
                        loc10.data.push(parseFloat(loc9.shift()));
                        continue;
                    }
                    if (loc11 != "Z") 
                    {
                        continue;
                    }
                    loc10.commands.push(GraphicsPathCommand.NO_OP);
                }
                loc6[loc7.@id] = loc10;
            }
            loc17 = 0;
            loc18 = svg.a;
            
            for each (loc8 in loc18) 
            {
            	loc12 = String(loc8.g.@transform);
                loc13 = loc12.substring(7, (loc12.length - 1)).split(",");
                loc14 = loc8.g.rect[0];
                
                tag = new Tag(new URLRequest(loc8.@xlink::href), new Rectangle(loc14.@x, loc14.@y, loc14.@width, loc14.@height), new Matrix(loc13[0], loc13[1], loc13[2], loc13[3], loc13[4], loc13[5]), new GraphicsSolidFill(this.getColor(loc8.g.@fill, 1)), loc5, this.viewBox);
                var loc19:*=0;
                var loc20:*=loc8.g["use"];
                for each (loc16 in loc20) 
                {
                    tag.addGlyph(loc6[loc16.@xlink::href.substr(1)], loc16.@x, loc16.@y);
                }
                addChildAt(tag, 0);
                addChild(new EventBox(tag, popup));
            }
            return;
        }

        public function get ratio():Number
        {
            return this.viewBox.width / this.viewBox.height;
        }

        public function resize(arg1:Number, arg2:Number):void
        {
            var loc1:*=Math.min(arg1 / this.viewBox.width, arg2 / this.viewBox.height);
            var loc2:*;
            scaleY = loc2 = loc1;
            scaleX = loc2;
            x = (arg1 - width) / 2 - this.viewBox.x * loc1;
            y = (arg2 - height) / 2 - this.viewBox.y * loc1;
            return;
        }

        private var viewBox:Rectangle;

        private var stringHelper:StringHelper;

        public var backgroundColor:Number;
    }
}


